﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Admin_seminar : System.Web.UI.Page
{
    myDataSetTableAdapters.SeminarsTableAdapter semAdapter = new myDataSetTableAdapters.SeminarsTableAdapter();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            display();
        }
    }

    protected void display()
    {
        myDataSet.SeminarsDataTable semTable = semAdapter.GetDataConvert();
        DataList1.DataSource = semTable;
        DataList1.DataBind();
    }

    protected void clear()
    {
        txtId.Text = "";
        txtTitle.Text = "";
        txtDes.Text = "";
        CKEditor1.Text = "";
        txtImage.Text = "";
        RadDatePicker1.SelectedDate = null;
        RadDatePicker2.SelectedDate = null;
        txtConduct.Text = "";
        lblResult.Text = null;
        lblTitleError.Visible = false;
        lblDesError.Visible = false;
        lblsDateError.Visible = false;
        lbleDateError.Visible = false;
        lblConductError.Visible = false;
    }

    protected bool checkEmpty()
    {
        bool status = true;
        if (txtTitle.Text.Trim() == "")
        {
            lblTitleError.Visible = true;
            status = false;
        }
        else
        {
            lblTitleError.Visible = false;
        }

        if (txtDes.Text.Trim() == "")
        {
            lblDesError.Visible = true;
            status = false;
        }
        else
        {
            lblDesError.Visible = false;
        }
        if (RadDatePicker1.SelectedDate == null)
        {
            lblsDateError.Visible = true;
            status = false;
        }
        else
        {
            lblsDateError.Visible = false;
        }

        if (RadDatePicker2.SelectedDate == null)
        {
            lbleDateError.Visible = true;
            status = false;
        }
        else
        {
            lbleDateError.Visible = false;
        }

        if (txtConduct.Text.Trim() == "")
        {
            lblConductError.Visible = true;
            status = false;
        }
        else
        {
            lblConductError.Visible = false;
        }
        return status;
    }

    protected void cmdSearchEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelInvi1.Visible = false;
        PanelInvi2.Visible = false;
        PanelInvi3.Visible = false;
        txtId.Enabled = true;
        txtId.BackColor = System.Drawing.Color.White;
        cmdSearch.Text = "Search";
        cmdClear.Visible = true;
        clear();
        if (cmdSearchEnable.Text == "Enable Search Area")
        {
            Panel1.Visible = true;
            cmdSearchEnable.Text = "Disable Search Area";
            cmdAddEnable.Text = "Enable Add Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdSearchEnable.Text = "Enable Search Area";
        }
    }

    protected void cmdAddEnable_Click(object sender, EventArgs e)
    {
        display();
        PanelInvi1.Visible = true;
        PanelInvi2.Visible = true;
        PanelInvi3.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Add new";
        cmdClear.Visible = true;
        clear();
        if (cmdAddEnable.Text == "Enable Add Area")
        {
            Panel1.Visible = true;
            cmdAddEnable.Text = "Disable Add Area";
            cmdSearchEnable.Text = "Enable Search Area";
        }
        else
        {
            Panel1.Visible = false;
            cmdAddEnable.Text = "Enable Add Area";
        }
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        if (cmdSearch.Text == "Search")
        {
            try
            {

                Panel1.Visible = true;
                string strSQL = "SELECT id, title, description, [content], image, startDate, endDate, conducted_by, CONVERT(varchar(10), startDate, 103) AS sDate, CONVERT(varchar(10), endDate, 103) AS eDate FROM Seminars where";
                strSQL += " title like N'%" + txtTitle.Text.Trim() + "%'";
                strSQL += " and conducted_by like N'%" + txtConduct.Text.Trim() + "%'";
                if (RadDatePicker1.SelectedDate != null)
                {
                    strSQL += " and CONVERT(varchar(10), startDate, 103) like '" + string.Format("{0:dd/MM/yyyy}", RadDatePicker1.SelectedDate.Value) + "'";
                }
                if (RadDatePicker2.SelectedDate != null)
                {
                    strSQL += " and CONVERT(varchar(10), endDate, 103) like '" + string.Format("{0:dd/MM/yyyy}", RadDatePicker2.SelectedDate.Value) + "'";
                }
                if (txtId.Text.Trim() != "")
                {
                    strSQL += " and id = " + txtId.Text.Trim();
                }
                string strConnection = ConfigurationManager.ConnectionStrings["EnvironmentSurveyConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(strConnection);
                SqlCommand sqlCmd = new SqlCommand(strSQL, conn);
                SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DataList1.DataSource = dt;
                DataList1.DataBind();
                if (dt.Rows.Count != 0)
                {
                    if (dt.Rows.Count == 1)
                    {
                        lblResult.Text = "There is 1 result";
                    }
                    else
                    {
                        lblResult.Text = "There are " + dt.Rows.Count + " results";
                    }
                }
                else
                {
                    lblResult.Text = "0 result... Please search again!";
                }
            }
            catch
            {
                RegisterStartupScript("as", "<script>alert('Input wrong format!')</script>");
            }
        }
        else if (cmdSearch.Text == "Add new")
        {
            if (checkEmpty())
            {
                semAdapter.InsertQuery(txtTitle.Text.Trim(), txtDes.Text.Trim(), CKEditor1.Text.Trim(), txtImage.Text.Trim(), RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value, txtConduct.Text.Trim());
                display();
            }
        }
        else
        {
            if (checkEmpty())
            {
                semAdapter.UpdateQuery(txtTitle.Text.Trim(), txtDes.Text.Trim(), CKEditor1.Text.Trim(), txtImage.Text.Trim(), RadDatePicker1.SelectedDate.Value, RadDatePicker2.SelectedDate.Value, txtConduct.Text.Trim(), int.Parse(txtId.Text.Trim()));
                display();
            }
        }
    }

    protected void cmdClear_Click(object sender, EventArgs e)
    {
        clear();
        Panel1.Visible = true;
    }

    protected void cmdHide_Click(object sender, EventArgs e)
    {
        display();
        Panel1.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        lblResult.Text = null;
    }

    protected void getDataById(int id)
    {
        myDataSet.SeminarsDataTable semTable = semAdapter.GetDataById(id);
        txtId.Text = semTable.Rows[0]["id"].ToString();
        txtTitle.Text = semTable.Rows[0]["title"].ToString();
        txtDes.Text = semTable.Rows[0]["description"].ToString();
        CKEditor1.Text = semTable.Rows[0]["content"].ToString();
        txtImage.Text = semTable.Rows[0]["image"].ToString();
        RadDatePicker1.SelectedDate = DateTime.Parse(semTable.Rows[0]["startDate"].ToString());
        RadDatePicker2.SelectedDate = DateTime.Parse(semTable.Rows[0]["endDate"].ToString());
        txtConduct.Text = semTable.Rows[0]["conducted_by"].ToString();
    }
  
    protected void lblEdit_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        Panel1.Visible = true;
        PanelInvi1.Visible = true;
        PanelInvi2.Visible = true;
        PanelInvi3.Visible = true;
        txtId.Enabled = false;
        txtId.BackColor = System.Drawing.Color.WhiteSmoke;
        cmdSearch.Text = "Save";
        cmdClear.Visible = false;
        cmdSearchEnable.Text = "Enable Search Area";
        cmdAddEnable.Text = "Enable Add Area";
        clear();
        getDataById(id);
    }

    protected void lblDelete_Command(object sender, CommandEventArgs e)
    {
        int id = int.Parse(e.CommandArgument.ToString());
        semAdapter.DeleteQuery(id);
        clear();
        display();
    }

    protected void cmdUploadImg_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("../Images/") + FileUpload1.FileName);
            txtImage.Text = "images/" + FileUpload1.FileName;
        }
        else
        {
            RegisterStartupScript("as", "<script>alert('Choose image first!')</script>");
        }
    }
}
